package base;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sessionHibernate.HibernateUtil;
import bean.Bateau;

public class Base {

	private Session sessionHibernate;


	public void ouvrirSession() {
		if (sessionHibernate == null) {
			sessionHibernate = HibernateUtil.getSession();
		}
	}

	public void fermerSession() {
		if (sessionHibernate != null) {
			try {
				sessionHibernate.close();
			} catch (Exception e) {
			}
		}
		sessionHibernate = null;
	}

	public Bateau chargerBateau(Integer id) {
		Transaction tx = sessionHibernate.beginTransaction();
		Bateau b = (Bateau)sessionHibernate.load(Bateau.class,id);
		tx.commit();
		return b;		
	}
	
	public Integer ajoutBateau(Bateau b) {
		Transaction tx = sessionHibernate.beginTransaction();
		sessionHibernate.save(b);
		tx.commit();
		return b.getId_bateau();

	}
	
	public void modifierBateau(Bateau b) {
		Transaction tx = sessionHibernate.beginTransaction();
		sessionHibernate.merge(b);
		tx.commit();

	}
	
	public void supprimerBateau(Bateau b) {
		Transaction tx = sessionHibernate.beginTransaction();
		sessionHibernate.delete(b);
		tx.commit();
		
	}
	
	public List<Bateau> listerBateaux() {
		Query query = sessionHibernate.createQuery("from " + Bateau.class.getName());
		List<Bateau> res = query.list();
		return res;
	}
	
	public static void main(String [] args) {
		Base base = new Base();
		base.ouvrirSession();
		base.fermerSession();
	}
}